home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / DBA / Driver / Builtin.php next >
PHP Script  |  2004-10-01  |  15KB  |  530 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2002-2003 Brent Cook                                        |
  5. // +----------------------------------------------------------------------+
  6. // | This library is free software; you can redistribute it and/or        |
  7. // | modify it under the terms of the GNU Lesser General Public           |
  8. // | License as published by the Free Software Foundation; either         |
  9. // | version 2.1 of the License, or (at your option) any later version.   |
  10. // |                                                                      |
  11. // | This library is distributed in the hope that it will be useful,      |
  12. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  14. // | Lesser General Public License for more details.                      |
  15. // |                                                                      |
  16. // | You should have received a copy of the GNU Lesser General Public     |
  17. // | License along with this library; if not, write to the Free Software  |
  18. // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Builtin.php,v 1.13 2003/01/27 04:31:42 busterb Exp $
  22.  
  23. require_once 'DBA.php';
  24.  
  25. /**
  26.  * DBA_Driver_Builtin uses the builtin dba functions of PHP as the underlying
  27.  * driver for a DBA class. Depending on the driver, this can be faster or
  28.  * slower than the DBA_Driver_File class.
  29.  *
  30.  * This class has been tested with DB3 and GDBM and probably works with DB2.
  31.  * Other drivers may have quirks that this class does not address yet. CDB
  32.  * is known to be unsuitable as a driver due to its lack of write ability.
  33.  *
  34.  * @author  Brent Cook <busterb@mail.utexas.edu>
  35.  * @version 1.0
  36.  * @access  public
  37.  * @package DBA
  38.  */
  39. class DBA_Driver_Builtin extends DBA
  40. {
  41.  
  42.     // {{{ instance variables
  43.     /**
  44.      * Name of the database
  45.      * @access private
  46.      */
  47.     var $_dbName;
  48.  
  49.     /**
  50.      * Indicates the current ability for read/write operations
  51.      * @access private
  52.      */
  53.     var $_writable;
  54.  
  55.     /**
  56.      * Indicates the current ability for read operations
  57.      * @access private
  58.      */
  59.     var $_readable;
  60.  
  61.     /**
  62.      * Name of the builtin dba driver to use
  63.      * @access private
  64.      */
  65.     var $_driver;
  66.  
  67.     /**
  68.      * Indicates the ability of the dba driver to replace values
  69.      * @access private
  70.      */
  71.     var $_hasReplace;
  72.     // }}}
  73.  
  74.     // {{{ DBA_Driver_Builtin($driver = 'gdbm')
  75.     /* Constructor
  76.      *
  77.      * @access public
  78.      * @param   string  $driver dba driver to use
  79.      */
  80.     function DBA_Driver_Builtin($driver = 'gdbm')
  81.     {
  82.         // call the base constructor
  83.         $this->DBA();
  84.         $this->_driver = $driver;
  85.     }
  86.     // }}}
  87.  
  88.     // {{{ open($dbName='', $mode='r', $persistent=false)
  89.     /**
  90.      * Opens a database.
  91.      *
  92.      * @access public
  93.      * @param   string  $dbName The name of a database
  94.      * @param   string  $mode The mode in which to open a database.
  95.      *                   'r' opens read-only.
  96.      *                   'w' opens read-write.
  97.      *                   'n' creates a new database and opens read-write.
  98.      *                   'c' creates a new database if the database does not
  99.      *                      exist and opens read-write.
  100.      * @param   boolean $persistent Determines whether to open the database
  101.      *                  peristently. Not supported here.
  102.      * @return  object PEAR_Error on failure
  103.      */
  104.     function open($dbName='', $mode='r', $persistent = false)
  105.     {
  106.         if (is_null($this->_driver)) {
  107.             return $this->raiseError(DBA_ERROR_NO_DRIVER);
  108.         }
  109.  
  110.         if ($this->_driver == 'gdbm') {
  111.             $this->_hasReplace = false;
  112.         } else {
  113.             $this->_hasReplace = true;
  114.         }
  115.  
  116.         if ($dbName == '') {
  117.             return $this->raiseError(DBA_ERROR_NO_DBNAME);
  118.         } else {
  119.             $this->_dbName = $dbName;
  120.         }
  121.  
  122.         switch ($mode) {
  123.             case 'r':
  124.                     // open for reading
  125.                     $this->_writable = false;
  126.                     $this->_readable = true;
  127.                     break;
  128.             case 'n':
  129.             case 'c':
  130.             case 'w':
  131.                     $this->_writable = true;
  132.                     $this->_readable = true;
  133.                     break;
  134.             default:
  135.                 return $this->raiseError(DBA_ERROR_INVALID_MODE, NULL, NULL,
  136.                     'filemode: '.$mode);
  137.         }
  138.  
  139.         // open the index file
  140.         if ($persistent) {
  141.             $this->_dba = dba_popen($dbName, $mode, $this->_driver);
  142.         } else {
  143.             $this->_dba = dba_open($dbName, $mode, $this->_driver);
  144.         }
  145.         if ($this->_dba === false) {
  146.             $this->_writable = false;
  147.             $this->_readable = false;
  148.             return $this->raiseError(DBA_ERROR_CANNOT_OPEN, NULL, NULL,
  149.                 'dbname: '.$dbName.' filemode: '.$mode);
  150.         }
  151.     }
  152.     // }}}
  153.  
  154.     // {{{ close()
  155.     /**
  156.      * Closes an open database.
  157.      *
  158.      * @access  public
  159.      * @return  object PEAR_Error on failure
  160.      */
  161.     function close()
  162.     {
  163.         if ($this->isOpen()) {
  164.             $this->_readable = false;
  165.             $this->_writable = false;
  166.             dba_sync($this->_dba); // db2 is known to require syncs
  167.             dba_close($this->_dba);
  168.         } else {
  169.             return $this->raiseError(DBA_ERROR_NOT_OPEN);
  170.         }
  171.     }
  172.     // }}}
  173.  
  174.     // {{{ reopen($mode)
  175.     /**
  176.      * Reopens an already open database in read-only or write mode.
  177.      * If the database is already in the requested mode, then this function
  178.      * does nothing.
  179.      *
  180.      * @access  public
  181.      * @param   string  $mode 'r' for read-only, 'w' for read/write
  182.      * @return  object PEAR_Error on failure
  183.      */
  184.     function reopen($mode)
  185.     {
  186.         if ($this->isOpen()) {
  187.             if (($mode == 'r') && $this->isWritable()) {
  188.                 // Reopening as read-only
  189.                 $this->close();
  190.                 return $this->open($this->_dbName, 'r');
  191.             } elseif (($mode == 'w') && (!$this->isWritable)) {
  192.                 // Reopening as read-write
  193.                 $this->close();
  194.                 return $this->open($this->_dbName, 'w');
  195.             }
  196.         } else {
  197.             return $this->raiseError(DBA_ERROR_NOT_OPEN);
  198.         }
  199.     }
  200.     // }}}
  201.  
  202.     // {{{ _DBA_Driver_Builtin()
  203.     /**
  204.      * PEAR emulated destructor calls close on PHP shutdown
  205.      * @access private
  206.      */
  207.     function _DBA_Driver_Builtin()
  208.     {
  209.         $this->close();
  210.     }
  211.     // }}}
  212.  
  213.     // {{{ isOpen()
  214.     /**
  215.      * Returns the current open status for the database
  216.      *
  217.      * @access  public
  218.      * @return  boolean true if open, false if closed 
  219.      */
  220.     function isOpen()
  221.     {
  222.         return($this->_readable || $this->_writable);
  223.     }
  224.     // }}}
  225.  
  226.     // {{{ isReadable()
  227.     /**
  228.      * Returns the current read status for the database
  229.      *
  230.      * @access  public
  231.      * @return  boolean true if readable, false if not
  232.      */
  233.     function isReadable()
  234.     {
  235.         return $this->_readable;
  236.     }
  237.     // }}}
  238.  
  239.     // {{{ isWritable()
  240.     /**
  241.      * Returns the current write status for the database
  242.      *
  243.      * @access  public
  244.      * @return  boolean true if writable, false if not
  245.      */
  246.      function isWritable()
  247.      {
  248.          return $this->_writable;
  249.      }
  250.     // }}}
  251.  
  252.     // {{{ remove($key)
  253.     /**
  254.      * Removes the value at location $key
  255.      *
  256.      * @access  public
  257.      * @param   string  $key key to delete
  258.      * @return  object PEAR_Error on failure
  259.      */
  260.     function remove($key)
  261.     {
  262.         if ($this->isWritable()) {
  263.             if (!dba_delete($key, $this->_dba)) {
  264.                 return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key);
  265.             }
  266.         } else {
  267.             return $this->raiseError(DBA_ERROR_NOT_WRITEABLE);
  268.         }
  269.     }
  270.     // }}}
  271.  
  272.     // {{{ fetch($key)
  273.     /**
  274.      * Returns the value that is stored at $key.
  275.      *
  276.      * @access  public
  277.      * @param   string $key key to examine
  278.      * @return  mixed the requested value on success, false on failure
  279.      */
  280.     function fetch($key)
  281.     {
  282.         if ($this->isReadable()) {
  283.             if (dba_exists($key, $this->_dba)) {
  284.                 return dba_fetch($key, $this->_dba);
  285.             } else {
  286.                 return $this->raiseError(DBA_ERROR_NOT_FOUND, NULL, NULL, 'key: '.$key);
  287.             }
  288.         } else {
  289.             return $this->raiseError(DBA_ERROR_NOT_READABLE);
  290.         }
  291.     }
  292.     // }}}
  293.  
  294.     // {{{ firstkey()
  295.     /**
  296.      * Returns the first key in the database
  297.      *
  298.      * @access  public
  299.      * @return  mixed string on success, false on failure
  300.      */
  301.     function firstkey()
  302.     {
  303.         if ($this->isReadable()) {
  304.             return dba_firstkey($this->_dba);
  305.         } else {
  306.             return false;
  307.         }
  308.     }
  309.     // }}}
  310.  
  311.     // {{{ size()
  312.     /**
  313.      * Calculates the size of the database in number of keys
  314.      *
  315.      * @access  public
  316.      * @return  int    number of keys in the database
  317.      */
  318.     function size()
  319.     {
  320.         $key = dba_firstkey($this->_dba);
  321.         $size = 0;
  322.         while ($key !== false) {
  323.             ++$size;
  324.             $key = dba_nextkey($this->_dba);
  325.         }
  326.         return $size;
  327.     }
  328.     // }}}
  329.  
  330.     // {{{ nextkey()
  331.     /**
  332.      * Returns the next key in the database, false if there is a problem
  333.      *
  334.      * @access  public
  335.      * @return  mixed string on success, false on failure
  336.      */
  337.     function nextkey()
  338.     {
  339.         if ($this->isReadable()) {
  340.             return dba_nextkey($this->_dba);
  341.         } else {
  342.             return false;
  343.         }
  344.     }
  345.     // }}}
  346.  
  347.     // {{{ getkeys()
  348.     /**
  349.      * Returns all keys in the database
  350.      *
  351.      * @access  public
  352.      * @return  mixed  array
  353.      */
  354.     function getkeys()
  355.     {
  356.         $keys = array();
  357.         if ($this->isReadable()) {
  358.             $key = $this->firstkey();
  359.             while ($key !== FALSE) {
  360.                 $keys[] = $key;
  361.                 $key = $this->nextkey($key);
  362.             }
  363.         }
  364.         return $keys;
  365.     }
  366.     // }}}
  367.  
  368.     // {{{ insert($key, $value)
  369.     /**
  370.      * Inserts a new value at $key. Will not overwrite if the key/value pair
  371.      * already exist
  372.      *
  373.      * @access public
  374.      * @param   string  $key key to insert
  375.      * @param   string  $value value to store
  376.      * @return  object PEAR_Error on failure
  377.      */
  378.     function insert($key, $value)
  379.     {
  380.         if ($this->isWritable()) {
  381.  
  382.             if ((!$this->_hasReplace && dba_exists($key, $this->_dba)) ||
  383.                 (!dba_insert($key, $value, $this->_dba))) {
  384.                 return $this->raiseError(DBA_ERROR_ALREADY_EXISTS, NULL, NULL,
  385.                     'key: '.$key);
  386.             }
  387.         } else {
  388.             return $this->raiseError(DBA_ERROR_NOT_WRITEABLE);
  389.         }
  390.     }
  391.     // }}}
  392.  
  393.     // {{{ replace($key, $value)
  394.     /**
  395.      * Inserts a new value at key. If the key/value pair
  396.      * already exist, overwrites the value
  397.      *
  398.      * @access public
  399.      * @param   $key    string the key to insert
  400.      * @param   $value  string the value to store
  401.      * @return  object  PEAR_Error on failure
  402.      */
  403.     function replace($key, $value)
  404.     {
  405.         if ($this->isWritable()) {
  406.  
  407.             if ($this->_hasReplace) {
  408.                 return dba_replace($key, $value, $this->_dba);
  409.             } else {
  410.                 $r = true;
  411.                 if (dba_exists($key, $this->_dba)) {
  412.                     $r = dba_delete($key, $this->_dba);
  413.                 }
  414.                 return $r && dba_insert($key, $value, $this->_dba);
  415.             }
  416.  
  417.         } else {
  418.             return $this->raiseError(DBA_ERROR_NOT_WRITEABLE);
  419.         }
  420.     }
  421.     // }}}
  422.     
  423.     // {{{ create($dbName, $driver='gdbm')
  424.     /**
  425.      * Creates a new database file if one does not exist. If it already exists,
  426.      * updates the last-updated timestamp on the database
  427.      *
  428.      * @access  public
  429.      * @param   string  $dbName the database to create
  430.      * @param   string  $driver the dba driver to use
  431.      * @return  object  PEAR_Error on failure
  432.      */
  433.     function create($dbName, $driver='gdbm')
  434.     {
  435.         $db = dba_open($dbName, 'n', $driver);
  436.         if (!(($db !== false) && dba_close($db))) {
  437.             return $this->raiseError(DBA_ERROR_CANNOT_CREATE, NULL, NULL,
  438.                 'dbname: '.$dbname);
  439.         }
  440.     }
  441.     // }}}
  442.  
  443.     // {{{ db_exists($dbName)
  444.     /**
  445.      * Indicates whether a database with given name exists
  446.      *
  447.      * @access  public
  448.      * @param   string  $dbName the database name to check for existence
  449.      * @return  boolean true if the database exists, false if it doesn't
  450.      */
  451.     function db_exists($dbName)
  452.     {
  453.         return file_exists($dbName);
  454.     }
  455.     // }}}
  456.  
  457.     // {{{ db_drop($dbName)
  458.     /**
  459.      * Removes a database from existence
  460.      *
  461.      * @access  public
  462.      * @param   string  $dbName the database name to drop
  463.      * @return  object  PEAR_Error on failure
  464.      */
  465.     function db_drop($dbName)
  466.     {
  467.         if (DBA_Driver_Builtin::db_exists($dbName)) {
  468.             if (!unlink($dbName)) {
  469.                 return $this->raiseError(DBA_ERROR_CANNOT_DROP, NULL, NULL,
  470.                     'dbname: '.$dbName);
  471.             }
  472.         } else {
  473.             return $this->raiseError(DBA_ERROR_NOSUCHDB, NULL, NULL,
  474.                 'dbname: '.$dbName);
  475.         }
  476.     }
  477.     // }}}
  478.  
  479.     // {{{ drop()
  480.     /**
  481.      * Removes the last open database from existence
  482.      *
  483.      * @access  public
  484.      * @return  object  PEAR_Error on failure
  485.      */
  486.     function drop()
  487.     {
  488.         $this->close();
  489.         return $this->db_drop($this->_dbName);
  490.     }
  491.     // }}}
  492.  
  493.     // {{{ exists($key)
  494.     /**
  495.      * Check whether key exists
  496.      *
  497.      * @access  public
  498.      * @param   string   $key
  499.      * @return  boolean true if the key exists, false if it doesn't
  500.      */
  501.     function exists($key)
  502.     {
  503.         return($this->isOpen() && dba_exists($key, $this->_dba));
  504.     }
  505.     // }}}
  506.  
  507.     // {{{ sync()
  508.     /**
  509.      * Synchronizes an open database to disk
  510.      * @access public
  511.      */
  512.     function sync()
  513.     {
  514.         return dba_sync($this->_dba);
  515.     }
  516.     // }}}
  517.  
  518.     // {{{ optimize()
  519.     /**
  520.      * Optimizes an open database
  521.      * @access public
  522.      */
  523.     function optimize()
  524.     {
  525.         return dba_optimize($this->_dba);
  526.     }
  527.     // }}}
  528. }
  529. ?>
  530.